const app = getApp();
Page({
	data: {
		resourceDomain: app.resourceDomain,
		g_iphone: '',
    select: false,
    isSec: false,
    balance: 0
	},
	navigator_click() {
		wx.navigateBack({
			changed: true
		});
	},
	step_click() {
		wx.chooseAddress({
			success: (res) => {
				app.address = res;
				this.setData({
					address: res
				})
			},
			fail: function(err) {
				console.log(err)
			}
		})
	},
	openSettingBack: function(e) {
		this.setData({
			opensettingbutton: false
		});
		var that = this;
		wx.getSetting({
			success(res) {
				if (!res.authSetting['scope.address']) {
					that.setData({
						opensettingbutton: true
					});
				}
			}
		})
	},
	onLoad: function(options) {
		var that = this;
		this.type = options.type;
		var sku = options.sku != 'undefined' ? options.sku:'0';
		this.goods_id = options.goods_id;
		this.prize_id = options.prize_id;
		this.id = options.id;
		
		this.setData({
			address: app.address,
			sku: sku,
			original: options.original,
			num:options.num,
			lottery: options.type == 'lottery' ? true : false
		});

		wx.getSetting({
			success(res) {
				if (!res.authSetting['scope.address']) {
					that.setData({
						opensettingbutton: true
					});
				}
			}
		})

		app.SystemInfo(this)
		app.request(app.apiDomain + 'index.php?c=mall&a=goodsdetail', {
			goods_id: options.goods_id,
			sku: sku,
			num:options.num,
			original_price:options.original
			// original_price:options.original
		}, function(res) {
			if (res.data.status == '1') {
				var goods = res.data.data;
				that.setData({
					goods: goods,
					type: that.type,
					num:options.num,
          total_price: goods.total_price
				});

				wx.setNavigationBarTitle({
					title: res.data.data.f_name,
				});

			}
		});

	},
	onShareAppMessage: function() {
		return app.onShareAppMessage();
	},
	formSubmit: function(e) {
		var that = this;

		if (!app.address.userName) {
			wx.showToast({
				title: '请选择收货地址',
				icon: 'none',
				duration: 2000
			})
			return;
		}
		//
		if(this.type == 'pond'){
			console.log(1)
			this.lottery_gain_request(this.id,this.goods_id)
		}else  if (this.type == 'lottery') { //自律抽奖
			app.request(app.apiDomain + "index.php?c=lottery&a=submit", {
				type: 'lottery',
				goods_id: that.goods_id,
				prize_id: that.prize_id,
				id: that.id,
				truename: app.address.userName,
				mobile: app.address.telNumber,
				remark: e.detail.value.remark,
				address: app.address.provinceName + app.address.cityName + app.address.countyName + app.address.detailInfo
			}, function(res) {
				console.log(res)
				wx.navigateTo({
					url: 'complete?order_no=' + res.data.data.order_no
				})
			});
		} else { //健康币和助力
			app.request(app.apiDomain + "index.php?c=mall&a=order", {
				goods_id: e.detail.target.dataset.goodsid,
				sku: e.detail.target.dataset.sku,
				form_id: e.detail.formId,
				truename: app.address.userName,
				mobile: app.address.telNumber,
				remark: e.detail.value.remark,
				original_price: that.data.original,
				num:that.data.num,
				address: app.address.provinceName + app.address.cityName + app.address.countyName + app.address.detailInfo,
        balance: that.data.balance
			}, function(res) {
				// console.log(res.data.data)
				var than = res;
				if (res.data.status == 0) {
					wx.showToast({
						title: res.data.info,
						icon: 'none',
						duration: 2000
					})
					return;
				} else if (res.data.status == 1) {
					wx.navigateTo({
						url: 'complete?order_no=' + res.data.data
					})
				} else if (res.data.status == 2) {
					wx.requestPayment({
						'timeStamp': res.data.data.timeStamp,
						'nonceStr': res.data.data.nonceStr,
						'package': res.data.data.package,
						'signType': res.data.data.signType,
						'paySign': res.data.data.paySign,
						'success': function(res) {},
						'fail': function(res) {
							wx.showToast({
								title: '支付失败',
								icon: 'none',
								duration: 2000
							})
						},
						'complete': function(res) {
							// console.log(res)
							if (res.errMsg == "requestPayment:ok") { // 调用支付成功
								wx.redirectTo({
									url: 'complete?order_no=' + than.data.data.order_no +'&type=2' //
								})
							} else if (res.errMsg == 'requestPayment:cancel') {
								// 用户取消支付的操作
								wx.showToast({
									title: '已取消支付',
									icon: 'none',
									duration: 2000
								})
							}
						}
					})
				} else if (res.data.status == 3) {
					wx.showToast({
						title: '未支付，正在前往我的订单',
						icon: 'none',
						duration: 2000
					})
					wx.navigateTo({
						url: 'list'
					})
				}
			});
		}
		//
	},
	getPhoneNumber(e) {
		console.log(e.detail.errMsg)
		console.log(e.detail.iv)
		console.log(e.detail.encryptedData)
	},
	onReady: function() {
		// 	  app.request(app.apiDomain + 'index.php?c=user&a=userbinding',{}, function (res) {
		// 		  console.log(res,1)
		// 	  });

	},
  select (e) {
    let that = this
    let flag = e.target.dataset.flag
    console.log(typeof flag)
    if (flag == 'true') {
      let price = that.data.goods.total_price - that.data.goods.f_user_money > 0 ? parseFloat(that.data.goods.total_price - that.data.goods.f_user_money).toFixed(2) : 0
      that.setData({
        select: true,
        isSec: true,
        balance: 1,
        total_price: price
      })
    } else {
      that.setData({
        select: false,
        isSec: false,
        balance: 0,
        total_price: that.data.goods.total_price
      })
    }
  },
	lottery_gain_request(e,f){
		var that=this
		app.request(
			app.apiDomain + 'index.php?c=pond&a=submit',{
				goods_id: f,
				id:e
			}, 
			function (res) {
				// console.log(res.data.info)
				if(res.data.status==1){
					wx.redirectTo({
						url: 'complete?order_no=' + res.data.data.order_no
					})
				}else{
					wx.showToast({
						title: res.data.info,
						icon: 'none',
						duration: 2000
					})
				}
			}
		);
	}

})
